package com.projectxxxxxxxxxx.widget;

import android.appwidget.AppWidgetManager;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.widget.RemoteViews;

import com.projectxxxxxxxxxx.Constants;
import com.projectxxxxxxxxxx.R;
import com.projectxxxxxxxxxx.animator.DoorAnimator;
import com.projectxxxxxxxxxx.screens.HardwareScreen;
import com.projectxxxxxxxxxx.screens.NetworkScreen;
import com.projectxxxxxxxxxx.screens.Screen;
import com.projectxxxxxxxxxx.screens.ToogleScreen;

public class WidgetManager {

    public static final String ACTION_WIDGET_UPDATE = Constants.AUTHORITY + ".action.WIDGET_UPDATE";
    public static final String ACTION_WIDGET_OPEN_DOOR = Constants.AUTHORITY + ".action.WIDGET_OPEN_DOOR";
    public static final String ACTION_WIDGET_CLOSE_DOOR = Constants.AUTHORITY + ".action.WIDGET_CLOSE_DOOR";
    public static final String ACTION_WIDGET_CHANGE_SCREEN_TO_NEXT = Constants.AUTHORITY + ".action.WIDGET_CHANGE_SCREEN_TO_NEXT";
    public static final String ACTION_WIDGET_CHANGE_SCREEN_TO_PREV = Constants.AUTHORITY + ".action.WIDGET_CHANGE_SCREEN_TO_PREV";

	private final Context context;
    private BroadcastReceiver receiver = null;
    private static Screen screen;
    private String widgetClassName;

    public WidgetManager(Context context) {
		this.context = context;
	}

	public WidgetModel getModel() {
        return null;
	}
	
	public void setWidgetClassName(String widgetClassName) {
		this.widgetClassName = widgetClassName;
	}

	public String getWidgetClassName() {
		return widgetClassName;
	}
	
    public boolean isOpenDoor() {
        return DoorAnimator.openDoor;
    }

    public void changeDoor(final boolean ind) {
        if (ind == false && receiver != null) {
            context.unregisterReceiver(receiver);
        } else {
            changeScreen();
        }
    }

    public void changeScreen() {

        IntentFilter intentfilter = new IntentFilter();

        if (receiver != null) {
            try {
                context.unregisterReceiver(receiver);
            } catch (Exception e) {
            }
        }

        if (screen != null) {
            screen.stopUpdate();
        }

        switch (Screen.currentScreen) {
            case 0:
                receiver = new HardwareBrodcast();
                intentfilter.addAction(Constants.ACTION_UPDATE_HARDWARE_SCREEN);
                screen = new HardwareScreen(context, Screen.currentScreen, getWidgetClassName());
                break;
            case 1:

                receiver = new NetworkBroadcast();
				intentfilter.addAction(Constants.ACTION_UPDATE_NETWORK_SCREEN);
				intentfilter.addAction(Constants.ACTION_NETWORK_SCREEN_WIFI_TRAFIC_CLEAR_CLICK);
				intentfilter.addAction(Constants.ACTION_NETWORK_SCREEN_GPRS_TRAFIC_CLEAR_CLICK);

                screen = new NetworkScreen(context, Screen.currentScreen);
                break;
            case 2:
                receiver = new ToogleBrodcast();
                intentfilter.addAction(Constants.ACTION_UPDATE_TOOGLE_ALL);
                intentfilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        		intentfilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        		intentfilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        		intentfilter.addAction(Constants.ACTION_TOGGLE_WIFI);
                intentfilter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
                intentfilter.addAction(Constants.ACTION_TOGGLE_SILENT);
                intentfilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
                intentfilter.addAction(Constants.ACTION_TOGGLE_BLUETOOTH);
                intentfilter.addAction(Constants.ACTION_TOGGLE_FLASH_LIGHT);
                intentfilter.addAction(Constants.ACTION_TOGGLE_AUTO_ROTATE);
                intentfilter.addAction(Constants.ACTION_ANY_DATA_STATE);
                intentfilter.addAction(Constants.ACTION_TOGGLE_DATA);
                intentfilter.addAction(Constants.ACTION_TOGGLE_MOBILE);
             
                // register your actions
                screen = new ToogleScreen(context, Screen.currentScreen);
                break;
        }

        if (receiver != null) {
            context.registerReceiver(receiver, intentfilter);
        }

        if (screen != null) {
            screen.updateAllView(new RemoteViews(context.getPackageName(), R.layout.appwidget));
        }
    }

    public int[] loadWidgetIds(Context context) {
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        ComponentName componentName = new ComponentName(context, WidgetReceiver.class);
        final int[] widgetIds = appWidgetManager.getAppWidgetIds(componentName);
        return widgetIds;
    }

    // public void updateWidget

    

	public class ToogleBrodcast extends BroadcastReceiver {
        @Override
        public void onReceive(final Context context, final Intent intent) {
            // screen = new ToogleScreen(context, Screen.currentScreen);

            if (screen instanceof ToogleScreen) {
                RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
                screen.updateView(intent, intent.getAction(), views);
            
            }

        }

    }

    public class HardwareBrodcast extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
        	 if (screen instanceof HardwareScreen) {
        		 RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
        		 screen.updateView(intent, intent.getAction(), views);
        	 }
        }

    }

    public class NetworkBroadcast extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {




            if (screen instanceof NetworkScreen) {
				RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);

				if (intent.getAction().equals(Constants.ACTION_NETWORK_SCREEN_GPRS_TRAFIC_CLEAR_CLICK)) {
					NetworkScreen.startGprsTraffic = true;
				} else if (intent.getAction().equals(Constants.ACTION_NETWORK_SCREEN_WIFI_TRAFIC_CLEAR_CLICK)) {
					NetworkScreen.startWifiTraffic = true;
				} else if (intent.getAction().equals(Constants.ACTION_UPDATE_NETWORK_SCREEN)) {
					 screen.updateView(intent, intent.getAction(), views);
				}
            }
        }
    }
}
